﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using GoodMan.Tool.Configration;

namespace GoodMan.Portal.WebLogic
{
    public class WarpAttribute : FilterAttribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var host = filterContext.RequestContext.HttpContext.Request.Url.Host;
            if (!(host == "localhost" || host == "127.0.0.1" || host == "112.124.48.184"))
            {
                if (new Regex("^([A-Za-z0-9\\-]+\\.){2}com$").IsMatch(host))
                {
                    string scope = host.Substring(0, host.IndexOf('.'));
                    if (scope == "www" || !SiteResource.DomainScope.Keys.Contains(
                        scope + "." + SiteResource.Domain))
                    {
                        filterContext.Result = new RedirectResult("/address/selectaddress");
                    }
                }
                else
                {
                    filterContext.Result = new RedirectResult("/address/selectaddress");
                }
            }
        }
    }
}
